/**

 */
package model
{
    import flash.events.EventDispatcher;

    import model.vo.CountryVO;

    import mx.collections.IList;

    import org.osmf.layout.PaddingLayoutMetadata;

    public class CountryDataProvider extends EventDispatcher implements IList
    {
        private var collection:Vector.<CountryVO> = new Vector.<CountryVO>;

        public function CountryDataProvider(collection:Vector.<CountryVO>)
        {
            this.collection = collection;
        }

        public function getItemByCode(countryCode:String):CountryVO
        {
            for (var i:int = 0; i < collection.length; i++)
            {
                if (collection[i].countryCode == countryCode)
                    return collection[i];
            }

            return null;
        }


        //---------------------------------------------------------------------
        //
        //  IList implementation
        //
        //---------------------------------------------------------------------

        public function get length():int
        {
            return collection.length;
        }

        public function addItem(item:Object):void
        {
            collection.push(item);
        }

        public function addItemAt(item:Object, index:int):void
        {
            collection.push(item); // TODO: insert at specified index; throw RangeError if index is out of range
        }

        public function getItemAt(index:int, prefetch:int=0):Object
        {
            return collection[index] as Object;
        }

        public function getItemIndex(item:Object):int
        {
            for (var i:int = 0; i < collection.length; i++)
            {
                var element:CountryVO = collection[i] as CountryVO;
                if(element == item)
                {
                    return i;
                }
            }
            return -1;
        }

        public function itemUpdated(item:Object, property:Object=null, oldValue:Object=null, newValue:Object=null):void
        {
            //TODO: implement function
            trace("itemUpdated NOT IMPLEMENTED");
        }

        public function removeAll():void
        {
            collection.splice(0, collection.length);
        }

        public function removeItemAt(index:int):Object
        {
            var removedStrings:Vector.<CountryVO> = collection.splice(index,1);
            return removedStrings[0];
        }

        public function setItemAt(item:Object, index:int):Object
        {
            if(index < 0 || index >= collection.length)
            {
                throw new RangeError("setItemAt index: " + index + " is out of range");
            }
            collection.splice(index, 0, item);
            return null;
        }

        public function toArray():Array
        {
            return null;
        }
    }
}
